<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NewsArticleController
 *
 * @author bien.tran
 */

namespace Blog\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\HttpFoundation\Request;
use Blog\BlogBundle\Entity\NewsArticles;
use Blog\BlogBundle\Entity\Tag;
use Blog\BlogBundle\Form\NewsSectionType;
use Blog\BlogBundle\Form\NewsCategoryType;
use Blog\BlogBundle\Form\NewsArticleType;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\ValidatorFactory;

class NewsArticleController extends Controller {

    public function indexAction() {
        return new Response("Welcome to blog");
    }

    public function viewArticleAction($id) {
        $item = $this->getDoctrine()->getEntityManager()->getRepository('BlogBundle:NewsArticles')->find($id);
        return $this->render('BlogBundle:news:articleDetail.html.twig', array('item' => $item));
    }

    public function listArticleAction() {
        $listItem = $this->getDoctrine()->getEntityManager()->getRepository('BlogBundle:NewsArticles')->findAll();
        return $this->render('BlogBundle:news:viewListArticle.html.twig', array('listItem' => $listItem));
    }

    public function addNewArticleAction(Request $request) {
        $newContent = $this->getDoctrine()->getEntityManager()->getRepository('BlogBundle:NewsArticles')->find(1);//new NewsArticles();
        //$newContent = $this->getDoctrine()->getEntityManager()->getRepository('BlogBundle:NewsArticles')->find(1);
        /*$tag1 = new Tag();
        $tag1->name = 'Sport';
        $tag2 = new Tag();
        $tag2->name = 'Traveler';
        $newContent->getTags()->add($tag1);
        $newContent->getTags()->add($tag2);*/
        $form = $this->createForm(new NewsArticleType(), $newContent); // index for options array: data, required, max_length
        if ($request->getMethod() == 'POST') {
            
            $form->bindRequest($request);/*
            $temp = $newContent->getTitle();
            print_r($temp);
            die(' ++');*/
            /*
              $validator = $this->get('validator');
              $errors = $validator->validate($newContent);
              if (count($errors) > 0) {
              print_r($errors); die('- agfdg');
              } */
            if ($form->isValid()) {
                //print_r($newContent->getTags()); die('-asdgsdg');
                $db = $this->getDoctrine()->getEntityManager();
                $newContent->setSection($newContent->getCategory()->getSection());
                $newContent->setCreatedAt(new \DateTime);
                $newContent->setAuthorId(1);
                $newContent->uploadImage($this->get('kernel')->getRootDir() . '/../web/file_upload/');
                $db->persist($newContent);
                $db->flush();
                $this->get('session')->setFlash('statusAdd', 'Added content successful!');
            }
        }
        //print_r($form->createView()); die('-d');
        return $this->render('BlogBundle:news:addNewArticle.html.twig', array('formart' => $form->createView()));
    }

    public function updateArticleAction($id) {
        return new Response("Welcome to blog");
    }

    public function viewCountryAction() {

        $client = new \Soapclient('http://services-sandbox.insing.com/api/1.0/authenticate?wsdl', array('trace' => 1));
        $login = $client->login(array('deviceIdentifier' => array('type' => '0', 'identifier' => '0'), 'clientId' => 'hgwinsing', 'password' => '2e1780e7b58fd057a3a20fcd391de022'));
        $login = get_object_vars($login);
        if ($login['resultCode'] == 200) {
            $login = $login['sessionToken'];
        }
        else
        {
            die('login faild');
        }
        $media = new \SoapClient('http://services-sandbox.insing.com/api/1.0/search/home?wsdl');
        $art = $media->findArticle(array('sessionToken' => $login, 'articleId' => '3574691'));
        
        print_r($art);
        print '<br /> <br />';
        
        /*
        $client = new \Soapclient('http://ws.cdyne.com/ip2geo/ip2geo.asmx?wsdl', array('trace' => 1));
        $client1 = new \Soapclient('http://www.webservicex.net/CurrencyConvertor.asmx?WSDL', array('trace' => 1));
        //$result_client = $client->__soapCall('ResolveIP', array('ipAddress' => '113.161.77.24', 'licenseKey' => ""));
        $result_client = $client->ResolveIP(array('ipAddress' => '113.161.77.24', 'licenseKey' => ""));
        $result_client2 = $client1->ConversionRate(array('FromCurrency' => 'USD', 'ToCurrency' => 'VND'));
        
        $result_client_1 = json_encode($result_client);
        print_r($result_client_1);
        print '<br /> <br />';
        $temp = new \SoapHeader();
        $temp->
        $result_client = get_object_vars($result_client);
        $result_client = get_object_vars($result_client['ResolveIPResult']);
        print_r($result_client);
        print '<br /> <br />';
        
        $result_client_1 = json_encode($result_client);
        print_r($result_client_1);
        print '<br /> <br />';
        
        print_r(json_encode($result_client2));
        print '<br /> <br />';
        */
        print_r($login);
        print '<br /> <br />';
        
        print_r($client->__getFunctions());
        print '<br /> <br />';
        
        print_r($media->__getFunctions());
        die(' -afdgf');
        //http://www.webservicex.net/country.asmx?WSDL
        //http://ws.cdyne.com/ip2geo/ip2geo.asmx?wsdl - find geo location on earth based on Zip code
        //http://www.webservicex.net/geoipservice.asmx?WSDL - get IP address of the persons computer
        //http://www.webservicex.net/CurrencyConvertor.asmx?WSDL - Get conversion rate from one currency to another currency 
    }

}

?>
